package com.tedu.bbs.controller;

import com.tedu.bbs.util.DBUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

@Controller
public class ArticleController {
    @RequestMapping("/articleList")
    public void articleList(HttpServletRequest request,HttpServletResponse response){
        System.out.println("开始处理发表文章");
        String uid=request.getParameter("userID");
        try {
            Connection connection=DBUtil.getConnection();

            String sql="SELECT a.title,a.u_id,u.username  " +
                       "FROM article a " +
                       "JOIN userinfo u " +
                       "ON a.u_id=u.id " +
                       "WHERE a.u_id=?";

            int id=Integer.parseInt(uid);
            PreparedStatement ps=connection.prepareStatement(sql);
            ps.setInt(1,id);
            ResultSet rs=ps.executeQuery();
                response.setContentType("text/html;charset=utf-8");
                PrintWriter pw = response.getWriter();
                pw.println("<!DOCTYPE html>");
                pw.println("<html lang=\"en\">");
                pw.println("<head>");
                pw.println("<body>");
                pw.println("<center>");
                pw.println("  <h1>文章列表</h1>");
                pw.println("  <table border=\"1\">");
                pw.println("    <tr>");
                pw.println("      <td>标题</td>");
                pw.println("      <td>作者</td>");
                pw.println("    </tr>");
                while (rs.next()) {
                   String title=rs.getString("a.title");
                   int aid=rs.getInt("a.u_id");
                   String name=rs.getString("u.username");
                    pw.println("    <tr>");
                    pw.println("      <td><a href='/articles?title="+title+"'>" +title+ "</td>");
                    //<a href='/articleList?userID="+id+"'>"
                    pw.println("      <td>" +name+ "</td>");
                    pw.println("    </tr>");
                }
                pw.println("  </table>");
                pw.println("</center>");
                pw.println("</body>");
                pw.println("</html>");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @RequestMapping("/writeArticle")
    public void writhe(HttpServletRequest request, HttpServletResponse response){
        String title=request.getParameter("title");
        String author=request.getParameter("author");
        String content=request.getParameter("content");
        if (title==null  || title.isEmpty() ||
            author==null || author.isEmpty()||
            content==null||content.isEmpty()){
            try {
                response.sendRedirect("article_error.html");
                return;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try (
                Connection connection= DBUtil.getConnection();
        ){
            String sql="SELECT id "+
                       "FROM userinfo "+
                       "WHERE username=?";

            PreparedStatement ps=connection.prepareStatement(sql);
            ps.setString(1,author);

            ResultSet rs=ps.executeQuery();
            if (rs.next()){
                int id=rs.getInt(1);

                String sql1="INSERT INTO article (title,content,u_id) VALUES (?,?,?)";
                PreparedStatement pss=connection.prepareStatement(sql1);
                pss.setString(1,title);
                pss.setString(2,content);
                pss.setInt(3,id);
                int num=pss.executeUpdate();
                if (num>0){
                    response.sendRedirect("article_success.html");
                    return;
                }
            }else {
                response.sendRedirect("article_fail.html");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @RequestMapping("/articles")
    public void articles(HttpServletRequest request,HttpServletResponse response){
        String title=request.getParameter("title");

        try {
                Connection connection=DBUtil.getConnection(
            );

            String sql="SELECT content " +
                    "FROM article " +
                    "WHERE title=? ";
            PreparedStatement ps=connection.prepareStatement(sql);
            ps.setString(1,title);
            ResultSet rs=ps.executeQuery();
            response.setContentType("text/html;charset=utf-8");
            PrintWriter pw=response.getWriter();
            pw.println("<!DOCTYPE html>");
            pw.println("<html lang=\"en\">");
            pw.println("<head>");
            pw.println("    <meta charset=\"UTF-8\">");
            pw.println("    <title>Title</title>");
            pw.println("</head>");
            pw.println("<body>");
            pw.println("<center>");
            pw.println("    <table border=\"1\">");
            pw.println("    <tr>");
            pw.println("  <h1>文章内容</h1>");
            pw.println("   </table>");
            pw.println("    </tr>");
            while (rs.next()){
                String content=rs.getString("content");
                pw.println("    <table border=\"1\">");
                pw.println("    <tr>");
                pw.println("  <td>"+content+"</td>");
                pw.println("    </tr>");
                pw.println("   </table>");
            }
            pw.println("</center>");
            pw.println("</body>");
            pw.println("</html>");

            rs.close();
            ps.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}








